package com.jcl.recruit.searchresume.service;

import com.jcl.core.utils.CollectionUtils;
import com.jcl.core.web.utils.RequestUtils;
import com.jcl.recruit.crawler.logic.CrawlerDownloadTaskLogic;
import com.jcl.recruit.crawler.model.CrawlerDownloadTask;
import com.jcl.recruit.resume.logic.ResumeBasicInfoLogic;
import com.jcl.recruit.resume.model.ResumeBasicInfo;
import com.jcl.recruit.server.elastic.SearchResumeServer;
import com.jcl.recruit.server.elastic.vo.SearchResumeResultEntity;
import com.jcl.recruit.user.logic.UserResumeAttentionLogic;
import com.jcl.recruit.user.model.UserResumeAttention;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SearchResumeService {

    @Autowired
    private SearchResumeServer searchResumeServer;

    @Autowired
    private ResumeBasicInfoLogic resumeBasicInfoLogic;

    @Autowired
    private CrawlerDownloadTaskLogic crawlerDownloadTaskLogic;

    @Autowired
    private UserResumeAttentionLogic userResumeAttentionLogic;

    public SearchResumeResultEntity findDetail(Integer id){

        String corpCode = RequestUtils.getCorpCode();
        Integer userId = Integer.parseInt(RequestUtils.getUserId());
        SearchResumeResultEntity entity = searchResumeServer.findEntity(id);
        if (null != entity) {
            if (null != userId) {
                List<UserResumeAttention> attentList = userResumeAttentionLogic.getListBySearchResumeId(userId, id);
                if (CollectionUtils.isNotEmpty(attentList)) {
                    entity.setAttention(true);
                }
            }
            Integer status = -1;
            ResumeBasicInfo resume = resumeBasicInfoLogic.findOneByExternalKey(corpCode, id.toString());
            if(null != resume){
                List<CrawlerDownloadTask> taskList = crawlerDownloadTaskLogic.findListByResumeId(corpCode, resume.getId());
                if(CollectionUtils.isNotEmpty(taskList)){
                    CrawlerDownloadTask task = taskList.get(0);
                    status = task.getStatus();
                }
            }
            entity.setDownStatus(status);
        }

        return entity;
    }


}
